package me.xplabs.utils 
{
	import flash.display.Graphics;
	import flash.geom.Point;
	import me.xplabs.gemo.Point2D;
	import me.xplabs.gemo.Point3D;
	/**
	 * ...
	 * @author xiaohan
	 */
	public class Utils 
	{
		/**
		 * 追踪输出消息
		 * @param	...arg
		 */
		public static function printMsg(...arg):void
		{
			//trace(arg);
		}
		public static const CORRECT:Number = Math.cos( -Math.PI / 6) * Math.SQRT2;
		/**
		 * convert3dTo2d 3d坐标转化2d坐标
		 * @param	pt3d 3d坐标
		 * @param	pt2d 2d坐标
		 */
		public static function convert3dTo2d(pt3d:Point3D,pt2d:Point2D):void
		{
			pt2d.setXY(pt3d.x - pt3d.y, (pt3d.x + pt3d.y) * .5 - pt3d.z * CORRECT);
		}
		/**
		 * convert2dTo3d 2d坐标转化3d坐标
		 * @param	pt2d 2d坐标
		 * @param	pt3d 3d坐标
		 */
		public static function convert2dTo3d(pt2d:Point2D, pt3d:Point3D):void
		{
			pt3d.setXYZ(pt2d.y + pt2d.x * .5, pt2d.y - pt2d.x * .5, 0);
		}
		/**
		 * 检测一点是否在椭圆内部
		 * @param	px 检测点的x坐标
		 * @param	py 检测点的y坐标
		 * @param	pw 长半轴
		 * @param	ph 短半轴
		 * @return 返回点是否在椭圆上及内部
		 */
		public static function checkPointInEllipse(px:int, py:int, pw:int, ph:int):Boolean
		{
			if (Math.abs(px) > pw || Math.abs(py) > ph) return false;
			return ((px / pw) * (px / pw) + (py / ph) * (py / ph)) <= 1;
		}
		/**
		 * 检测一点是否在圆内部
		 * @param	pt1 点1
		 * @param	pt2 点2
		 * @param	area 范围
		 * @return
		 */
		public static function checkPointInCircle(pt1:Point, pt2:Point, area:int):Boolean
		{
			var dx:Number = pt1.x - pt2.x;
			var dy:Number = pt1.y - pt2.y;
			if (Math.sqrt(dx * dx + dy * dy) <= area) return true;
			return false;
		}

		public static function drawCircleMask(angle:Number, g:Graphics, radius:int):void
		{
			var tempAngle:int = int(angle * 360);
			if (tempAngle >= 360) return;
			
			var angleBuf:Number = tempAngle * Math.PI / 180;
			g.beginFill(0x000000, .6);
			g.moveTo(radius, radius);
			g.lineTo(radius, 0);
			
			if (tempAngle < 45) {
				g.lineTo(0, 0);
				g.lineTo(0, radius * 2);
				g.lineTo(radius * 2, radius * 2);
				g.lineTo(radius * 2, 0);
				g.lineTo(radius * (1 + Math.tan(angleBuf)), 0);
			} else if (tempAngle < 135) {
				g.lineTo(0, 0);
				g.lineTo(0, radius * 2);
				g.lineTo(radius * 2, radius * 2);
				g.lineTo(radius * 2, radius * (1 - 1 / Math.tan(angleBuf)));
			} else if (tempAngle < 225) {
				g.lineTo(0, 0);
				g.lineTo(0, radius * 2);
				g.lineTo(radius * (1 - Math.tan(angleBuf)), radius * 2);
			} else if (tempAngle < 315) {
				g.lineTo(0, 0);
				g.lineTo(0, radius * (1 + 1 / Math.tan(angleBuf)));
			} else {
				g.lineTo(radius * (1 + Math.tan(angleBuf)), 0);
			}
			g.lineTo(radius, radius);
			g.endFill();
		}
		public static function directionStatus(angle:Number):int
		{
			//print(angle);
			var status:int = 0;
			//if (angle < 0) trace(angle);
			//if (angle > 180) trace(angle);
			if (angle > -45 && angle <= 45) status = 1;
			if (angle > 45 && angle <= 135) status = 4;
			if ((angle > 135 && angle <= 180) || (angle >= -180&&angle <= -135)) status = 2;
			if (angle > -135 && angle <= -45) status = 3;
			return status;
		}
		/**
		 * 转换为html格式
		 * @param pkey 需要转换的字符串
		 * @param color 字体颜色
		 * @param size 字体大小
		 * @param bold 是否加粗
		 * @param space 字体间隔
		 * @return 转换后的字符
		 */
		public static function strToHtml(pkey : String, color : String = "", size : int = 12, bold : Boolean = false, space : int = -1) : String {
			var str : String = "";
			for (var i : int = 0; i < space; i++) {
				str += " ";
			}
			str += pkey;
			str = "<font size='" + size + "' color='" + color + "'>" + str + "</font>";
			if (bold) str = "<b>" + str + "</b>";
			return str;
		}
	}
}